package com.springcloud.market.order.service.impl;

import com.springcloud.market.order.feign.StockFeignService;
import com.springcloud.market.order.mapper.OrderMapper;
import com.springcloud.market.order.pojo.Order;
import com.springcloud.market.order.service.OrderService;
import com.springcloud.market.order.service.OrderTimeoutSender;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j

public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderTimeoutSender orderTimeoutSender;

    @Autowired
    private StockFeignService stockFeignService;
    @GlobalTransactional
    @Override
    public Order create(Order order) {
        orderMapper.insert(order);
        String msg = stockFeignService.seataReduce(order.getProductId());
        System.out.println(msg);
        // 故意异常：余数不能为0
//        int i = 1/0;
        return order;
    }
    @Override
    public Order createOrder(){
        Integer orderId =202;
        try{
            orderTimeoutSender.sendOrderTimeoutMessage(orderId.toString());
        }catch (Exception e){
            System.err.println("发送超时消息失败："+e.getMessage());
        }
        return new Order(orderId,203);
    }


}
